package com.example.weather.Activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weather.DB.Dbmanager;
import com.example.weather.R;

public class MoreActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView more_iv_back;
    private TextView more_tv_exchangebg,more_tv_version,more_tv_cache,more_tv_share;
    private RadioGroup more_rg;
    private SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        more_iv_back=findViewById(R.id.more_iv_back);
        more_iv_back.setOnClickListener(this);
        more_tv_exchangebg=findViewById(R.id.more_tv_exchangebg);
        more_tv_exchangebg.setOnClickListener(this);
        more_rg=findViewById(R.id.more_rg);
        more_tv_version=findViewById(R.id.more_tv_version);
        more_tv_cache=findViewById(R.id.more_tv_cache);
        more_tv_share=findViewById(R.id.more_tv_share);
        more_tv_cache.setOnClickListener(this);
        more_tv_share.setOnClickListener(this);
        String version=getVersion();
        more_tv_version.setText("当前版本号： v"+version);
        pref = getSharedPreferences("bg_pref", MODE_PRIVATE);
        setRGListener();
    }

    private void setRGListener() {
        //改变背景图片的单选按钮监听
        more_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //获取目前的默认壁纸
                int bg = pref.getInt("bg", 0);
                SharedPreferences.Editor edit = pref.edit();
                Intent intent=new Intent(MoreActivity.this,MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);

                switch (checkedId) {
                    case R.id.more_rb_green:
                        if (bg==0) {
                            Toast.makeText(MoreActivity.this,"您选择的是当前背景，无需改变",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        edit.putInt("bg",0);
                        edit.commit();
                        break;
                    case R.id.more_rb_pink:
                        if (bg==1) {
                            Toast.makeText(MoreActivity.this,"您选择的是当前背景，无需改变",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        edit.putInt("bg",1);
                        edit.commit();
                        break;
                    case R.id.more_rb_blue:
                        if (bg==2) {
                            Toast.makeText(MoreActivity.this,"您选择的是当前背景，无需改变",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        edit.putInt("bg",2);
                        edit.commit();
                        break;
                }
                startActivity(intent);
            }
        });
    }

    private String getVersion() {
        //获取应用的版本号
        PackageManager manager = getPackageManager();
        String versionname=null;
        try {
            PackageInfo packageInfo = manager.getPackageInfo(getPackageName(), 0);
            versionname=packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionname;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more_iv_back:
                finish();
                break;
            case R.id.more_tv_exchangebg:
                if (more_rg.getVisibility()== View.VISIBLE) {
                    more_rg.setVisibility(View.GONE);
                }else
                    more_rg.setVisibility(View.VISIBLE);
                break;
            case R.id.more_tv_cache:
                clearcache();
                break;
            case R.id.more_tv_share:
                sharesoftware("这是航酱的天气App，测试版");
                break;
        }
    }

    private void sharesoftware(String s) {
        //分享软件的函数
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,s);
        startActivity(Intent.createChooser(intent,"航酱的天气"));
    }

    private void clearcache() {
        //清楚缓存的函数
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("确定要删除所有缓存？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Dbmanager.deleteall();
                Toast.makeText(MoreActivity.this,"已删除全部缓存",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MoreActivity.this,MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }).setNegativeButton("取消",null);
        builder.create().show();
    }
}
